package org.yxjme.ijkplayerlibrary.android;

import android.app.Service;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;

import java.lang.reflect.Field;


/**
 * 获取某一帧的视频
 *
 */
public class VideoUtil {


    private MediaMetadataRetriever mediaMetadataRetriever ;


    public VideoUtil(String path){
        mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(path);
    }



    /**
     * 视频是路径
     */
    public Bitmap getFrameAtTime(){
        Bitmap b = null ;
        if (mediaMetadataRetriever!=null){
             b = mediaMetadataRetriever.getFrameAtTime();
        }
        return b ;
    }




    /**
     * 视频是路径
     */
    public Bitmap getFrameAtTime(long timeUs){
        Bitmap b = null ;
        if (mediaMetadataRetriever!=null){
            b = mediaMetadataRetriever.getFrameAtTime(timeUs);
        }
        return b ;
    }





    /**
     * 设为媒体静音
     *
     * @param context
     */
    public static void silentSwitchOn(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE , 0);
    }



    /**
     * 取消媒体静音
     * @param context
     */
    public static void silentSwitchOff(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_UNMUTE, 0);
    }

    /**
     * 关闭当前视频的音量
     * @param mediaPlayer
     */
    public static void closeVideoVoc(MediaPlayer mediaPlayer){
        if (mediaPlayer!=null)
            mediaPlayer.setVolume(0f,0f);
    }


    /**
     * 关闭当前视频的音量
     * @param mediaPlayer
     */
    public static void openVideoVoc( Context context ,MediaPlayer mediaPlayer){
        if (mediaPlayer!=null){
//            AudioManager audioManager=(AudioManager)context.getSystemService(Service.AUDIO_SERVICE);
//            mediaPlayer.setAudioStreamType(AudioManager.STREAM_SYSTEM);
//            mediaPlayer.setVolume(audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM),
//                    audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM));
//            mediaPlayer.start();
        }
    }



    /**
     * @param volume 音量大小
     * @param object VideoView实例
     * */
    public static void setVolume(float volume, Object object) {
        try {
            Class<?> forName = Class.forName("android.widget.VideoView");
            Field field = forName.getDeclaredField("mMediaPlayer");
            field.setAccessible(true);
            MediaPlayer mMediaPlayer = (MediaPlayer) field.get(object);
            mMediaPlayer.setVolume(volume, volume);
        } catch (Exception e) {
        }
    }
}
